package org.dizem.groupon.action;


import com.opensymphony.xwork2.Preparable;
import org.dizem.groupon.model.domain.MerchantEntity;
import org.dizem.groupon.service.MerchantService;
import org.dizem.util.Pager;

import java.util.List;

/**
 * User: dizem@126.com
 * Time: 11-6-28 下午3:18
 */
public class MerchantAction extends BaseAction implements Preparable {
	private MerchantService service;
	private List<MerchantEntity> merchants;
	private MerchantEntity merchant;
	private int id;

	public MerchantEntity getMerchant() {
		return merchant;
	}

	public void setMerchant(MerchantEntity merchant) {
		this.merchant = merchant;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public MerchantService getService() {
		return service;
	}

	public void setService(MerchantService service) {
		this.service = service;
	}

	public List<MerchantEntity> getMerchants() {
		return merchants;
	}

	public void setMerchants(List<MerchantEntity> merchants) {
		this.merchants = merchants;
	}

	public void prepare() throws Exception {
		merchants = service.loadAll();
	}

	public String add() throws Exception {
		service.save(merchant);
		return super.execute();
	}

	public String edit() throws Exception {
		merchant = service.getById(id);
		return super.execute();
	}

	public String update() throws Exception {
		service.update(merchant);
		return super.execute();
	}

	public String delete() throws Exception {
		MerchantEntity merchant = service.getById(id);
		if (merchant != null)
			service.delete(merchant);
		return SUCCESS;
	}

	public String listMerchant() throws Exception {
		if (this.getRecordCount() < 0) {
			setRecordCount(service.getCount());
		}
		if (getPage() == null) {
			setPage(1);
		}
		Pager p = new Pager(getPage(), getRecordCount(), getPageSize());
		setPageCount(p.getPageCount());
		setPageUrl("listMerchant");
		setPage(p.getPage());
		merchants = service.load(p.getStartPage(), this.getPageSize());
		return SUCCESS;
	}
}
